<?php
/**
 * URL Rewrite - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: class_rewrite.php 539 2010-07-20 08:42:48Z lisijie86 $
*/

class Rewrite {
	
	var $mode;
	var $suffix = '';
	var $blogdir;
	var $route = array(
		'index' => 'page',
		'post' => 'alias|id',
		'page' => 'alias|id',
		'cate' => 'alias|id,page',
		'attach' => 'id',
		'archives' => 'date,page',
		'tags' => 'tagname,page',
		'comments' => 'page',
		'search' => 'sid,page'
	);
	var $regexp = array(
		'alias' => '/^[a-z]+[a-z0-9\-_\.]+$/i',
		'id' => '/^\d+$/i',
		'date' => '/^(\d{4})|(\d{4}\-\d{2})|(\d{4}\-\d{2}\-\d{2})$/',
		'page' => '/^\d+$/',
	);
	
	function Rewrite() {
		$this->__construct();	
	}
	
	function __construct() {
		$this->mode = config('urlmode');
		if ( config('urlsuffix') ) {
			$this->suffix = '.html';
		}
		$this->blogdir = config('blogdir');
	}
	
	function parse() {
		$method = 'parse_'.$this->mode;
		if ( method_exists($this, $method) ) {
			return $this->$method();
		}
	}
	
	function url($string) {
		$method = 'url_'.$this->mode;
		if ( method_exists($this, $method) ) {
			return $this->$method($string);
		}		
	}
	
	//普通模式的URL组装
	function url_0($string) {
		parse_str($string, $params);
		switch ( $params['mod'] ) {
			case 'index' : 
				$url = $params['page']?'?page='.$params['page']:''; break;
			case 'cate' :
				$url = '?cate='.$params['id'].($params['page']?'&page='.$params['page']:''); break;
			case 'post' :
				$url = '?p='.$params['id']; break;
			case 'page' :
				$url = '?pageid='.$params['id']; break;
			case 'tags' : 
				if ( !empty($params['tagname']) ) {
					$url = '?tag='.$params['tagname'].($params['page']?'&page='.$params['page']:'');
				} else {
					$url = '?tags';
				}
				break;
			case 'attach' :
				$url = '?attach='.$params['id']; break;
			default:
				$url = '?'.$string;
		}
		return $url;
	}
	
	//普通模式URL解析
	function parse_0() {
		$get = $_GET;
		$keys = array_keys($get);
		switch ( $keys[0] ) {
			case 'p' : 
				$get['mod'] = 'post'; $get['id'] = $get['p']; break;
			case 'pageid' :
				$get['mod'] = 'page'; $get['id'] = $get['pageid']; break;
			case 'cate' :
				$get['mod'] = 'cate'; $get['id'] = $get['cate']; break;
			case 'tags' :
			case 'tag' :
				$get['mod'] = 'tags'; $get['tagname'] = $get['tag']; break;
			case 'attach' :
				$get['mod'] = 'attach'; $get['id'] = $get['attach']; break;
			default :
				if ( empty($get['mod']) ) $get['mod'] = 'index'; break;
		} 
		return $get;
	}
	
	/**
	 * Rewrite模式的URL组装
	 * 
	 * 如果路由设置中存在该模块的设置，按照设置的变量顺序进行组装
	 * 否则组装成  /模块/变量1/值1/变量2/值2 的形式
	 *
	 * @param string $string  参数字符串，如：mod=post&id=123&alias=name
	 * @return string 组装好的URL
	 */
	function url_1 ($string) {
		parse_str($string, $params);
		$temp = array();
		$temp[] = $params['mod'];
		if ( isset($this->route[$temp[0]]) ) {
			$vars = explode(',',$this->route[$temp[0]]);
			foreach ( $vars as $var ) {
				if ( false !== strpos($var,'|') ) {
					$var = explode('|', $var);
					foreach ( $var as $v ) {
						if ( !empty($params[$v]) ) {
							$temp[] = $params[$v];
							break;
						}
					}
				} else {
					if ( isset($params[$var]) ) {
						$temp[] = $params[$var];
					}
				}
			}
		} else {
			unset($params['mod']);
			foreach ( (array)$params as $var => $val ) {
				$temp[$var] = $val;
			}
		}
		$url = $this->blogdir.implode('/',$temp).$this->suffix;
		return $url;
	}
	
	/**
	 * Rewrite模式的URL解析
	 *
	 * @return string 返回包含参数和对应值的关联数组
	 */
	function parse_1() {
		$querystring = $_SERVER['QUERY_STRING'];
		$get = array('mod'=>'index','page'=>1);
		if ( !empty($querystring) ) {
			if ( strpos($querystring, '#') !== false ) {
				$querystring = strstr($querystring,'#',true);
			}
			$querystring = trim($querystring, '/');
			if ( substr($querystring, 0 - strlen($this->suffix)) == $this->suffix ) {
				$querystring = substr($querystring, 0, 0 - strlen($this->suffix));
			}
			$querystring = explode('/', $querystring);
			//存在规则则按规则解析
			if ( isset($this->route[$querystring[0]]) ) {
				$get['mod'] = $querystring[0];
				$vars = explode(',',$this->route[$querystring[0]]);
				if ( $vars ) {
					foreach ( $vars as $key => $var ) {
						$value = $querystring[$key + 1];
						if ( false !== strpos($var,'|') ) {
							$var = explode('|', $var);
							foreach ( $var as $v ) {
								if ( isset($this->regexp[$v]) && preg_match($this->regexp[$v], $value) ){
									$var = $v;
									break;
								}
							}
						}
						$get[$var] = $value;
					}
				}
			//否则按照/module/var1/value1/var2/value2模式解析
			} else {
				if ( false !== strpos($_SERVER['QUERY_STRING'],'mod=') ) {
					return $_GET;
				}
				$get['mod'] = $querystring[0];
				$count = count($querystring);
				for ( $i=1; $i<$count; $i++ ) {
					$get[$querystring[$i]] = $querystring[++$i];
				}
			}
		}
		return $get;		
	}
}
?>